Re: filemaker to pgsql ? - Mailing list pgsql-general

From Michelle Murrain
Subject Re: filemaker to pgsql ?
Date
Msg-id a05001905b62e0382c966@[192.168.1.10]
Whole thread Raw
In response to filemaker to pgsql ?  (Isaac <Isaac@UNSTOPPABLE.com>)
List pgsql-general
At 3:06 PM -0500 11/6/00, Isaac wrote:
>hello,
>
>has anyone converted files from filemaker to postgres? I'm figuring I'll
>just export everything into tabbed text files and then use the perl
>extensions to parse it out into INSERT queries. If anyone has any experience
>(or code!) to share on this process, it would be great to hear about it.
>
>--i

Here is a very unfinished/unpolished (but working) script to insert
comma delimited files into postgres tables using perl:

#!/usr/bin/perl
#
# Tool to import tab or comma delimited files into a database
#
# Start with comma
#
#print "Filename:";
#chomp ($file = <>);

use DBI;

$file = "participants.txt";

open (FILE, "$file");
@lines = <FILE>;

#assume the first row are column names that conform to db field names
$colnames = shift (@lines);

@cols = split (',', $colnames);
$num_cols = $#cols;

$dbname = "st";
$table = "profile";

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname","","") or die "Can't
connect to database\n";

foreach (@lines) {

     #Put the lines in a hash
     %rows = (); $i=0;
     @data = split (',');
     foreach $col (@cols) {
         $rows{$col} = $data[$i];
         $i++;
     }


     # Build a SQL statement to insert each line from file

     $i=0;
     $sql0 = "INSERT INTO $table (";
     @sqla = (); @sqlb = ();
     foreach $key (keys %rows) {
             $sqla[$i] = $key;
             $sqlb[$i] = $rows{$key};
             $i++;
         }

      $sql1 = join (',',@sqla);
      $sql2 = ") VALUES ('";
      $sql3 = join ('\',\'',@sqlb);
      $sql4 = "')";
      $sql = "$sql0$sql1$sql2$sql3$sql4";

      print "SQL:$sql\n";



      my $sth = $dbh->prepare("$sql") or die "Can't prepare SQL
statement: $DBI::errstr\n";
      $sth->execute or die "Can't execute: $DBI::errstr\n";
      $sth->finish;

}

$dbh->disconnect;
--
--------------------------------
Michelle Murrain Ph.D., President
Norwottuck Technology Resources
mpm@norwottuck.com
http://www.norwottuck.com

pgsql-general by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: libpq.so.2.0 problem
Next
From: Bruce Guenter
Date:
Subject: Re: Expectations of MEM requirements for a DB with